﻿package picPoll 
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import com.greensock.*;
	import com.greensock.easing.*;
	/**
	 * 读取图片,并显示
	 * @author zhengzihua
	 */
	public class LoadPic extends Sprite
	{
		private var loader:Loader;
		public function LoadPic() 
		{
			loader=new Loader();
		}
		public function load(url:String):void{
			loader.load(new URLRequest(url));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
		}
		private function onComplete(e:Event):void{
			addChild(loader.content);
			var mc:DisplayObject=loader.content;
			var sourceW:int=loader.content.width;
			var sourceH:int=loader.content.height;
			var size:Number=suitSize(StaticInfo.PIC_WIDTH,StaticInfo.PIC_HEIGHT,sourceW,sourceH);
			size=int(size*100)/100;	
			
			mc.scaleX=size;
			mc.scaleY=size;
			
			mc.alpha=0;
			showEffect();
			
			mc.x=int((StaticInfo.PIC_WIDTH-mc.width)/2);
			mc.y=int((StaticInfo.PIC_HEIGHT-mc.height)/2);
			
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			dispatchEvent(e);
		}
		private function showEffect():void{
			TweenMax.to(loader.content, 1, {autoAlpha:1});
		}
		private function suitSize (w:int,h:int,sourceW:int,sourceH:int):Number{
			var size:Number=w/h;
			var sourceSize:Number=sourceW/sourceH;
			if(size>sourceSize){
				return h/sourceH
			}else if(size<sourceSize){
				return w/sourceW;
			}else{
				return 1;
			}
			//return null;
		}
		
	}

}